package com.zkzx.company.api.service.order;

import com.zkzx.common.company.model.CompanyDriverVO;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.driver.model.DriverVO;
import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.constants.OrderUsualEnum;
import com.zkzx.common.freight.model.OrderListVO;
import com.zkzx.common.freight.model.OrderVO;
import com.zkzx.common.freight.param.OrderFilterParam;
import com.zkzx.common.global.Global;
import com.zkzx.common.truck.model.TruckVO;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.company.api.client.company.CompanyClient;
import com.zkzx.company.api.client.driver.DriverClient;
import com.zkzx.company.api.client.order.OrderClient;
import com.zkzx.company.api.client.truck.TruckClient;
import com.zkzx.company.api.client.wh.WhAdaptorClient;
import com.zkzx.util.date.DateUtil;
import com.zkzx.util.json.JSONResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/13/2:01
 * @Description:
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private OrderClient orderClient;
    @Autowired
    private DriverClient driverClient;
    @Autowired
    private TruckClient truckClient;
    @Autowired
    private CompanyClient companyClient;
    @Autowired
    private WhAdaptorClient whAdaptorClient;



    public PageResult<OrderListVO> find(OrderFilterParam filterParam,RequestFilterParams params, Integer userId, Integer companyId)  {

        filterParam.setCompanyId(companyId);

        PageResult<OrderListVO> pageResult = orderClient.find(JSONResultUtil.object2JSON(filterParam),JSONResultUtil.object2JSON(params));

        List<OrderListVO> result = pageResult.getResult();

        if (CollectionUtils.isEmpty(result)){
            return new PageResult<>(params.getPage(),params.getPerPage(), Collections.emptyList(), Global.ZERO);
        }

        result.forEach(orderListVO->{

            List<CompanyDriverVO> companyDriverVOs = companyClient.getByDriverIdAndCompanyId(Integer.parseInt(orderListVO.getDriverId()), orderListVO.getCompanyId());
            if (CollectionUtils.isNotEmpty(companyDriverVOs)){
                orderListVO.setDriverMobile(companyDriverVOs.get(0).getMobile());
            }
            orderListVO.setTruckVO(truckClient.getById(Integer.parseInt(orderListVO.getTruckId()),companyId,userId));

        });

        return pageResult;

    }
    /**
     * 根据id获取订单信息
     * @param id 订单id
     * @return 订单详情信息
     */
    public OrderVO getById(Integer id,Integer userId,Integer companyId){
        OrderVO orderVO = orderClient.getById(id);

        if (Objects.isNull(orderVO)){
            return null;
        }
        //司机收款信息，电话，车辆信息
        DriverVO driverVO = driverClient.getById(orderVO.getDriverId(), companyId);

        if (Objects.nonNull(driverVO)){
            orderVO.setDriverVO(driverVO);
            if (null !=driverVO.getTruckId()){
                TruckVO truckVO = truckClient.getById(driverVO.getTruckId(), companyId, userId);
                driverVO.setTruck(truckVO.getHeaderLicenseVO().getLicence());
            }
        }
        //确认是否为常用车辆
        List<CompanyDriverVO> companyDriverVOS = companyClient.getByDriverIdAndCompanyId(orderVO.getDriverId(), companyId);
        if (CollectionUtils.isNotEmpty(companyDriverVOS)){
            CompanyDriverVO companyDriverVO = companyDriverVOS.get(Global.ZERO);
            if (Objects.nonNull(companyDriverVO.getTruckId()) && Objects.nonNull(orderVO.getTruckId()) && orderVO.getTruckId().equals(companyDriverVO.getTruckId()) ){
                 orderVO.setUsual(OrderUsualEnum.YES.name());
            }

        }
        orderVO.setTruckVO(truckClient.getById(orderVO.getTruckId(),companyId,userId));
        return orderVO;

    }


    /**
     * 新增运单信息
     * @param command 入参
     * @param userId 用户id
     * @param companyId 企业id
     */
    public StatusVO add(OrderCommand command, int userId, int companyId){

        //更新留存比例
         CompanyVO companyVO = companyClient.getById(companyId);
         command.getOrderPayableVO().setKeepFareproPortion(companyVO.getCompanyOperation().getFareScale());
        command.setCompanyId(companyId);
        return  orderClient.add(command, userId);

    }

    /**
     * 更新运单信息
     * @param id 运单id
     * @param command 入参
     * @param companyId 企业id
     * @param userId 登录人id
     */
    public void update(Integer id,OrderCommand command, int userId, int companyId) {
        command.setCompanyId(companyId);
        orderClient.update(id,command,userId);
    }


    /**
     * 提交运单信息到网货
     * @param id 运单id
     */
    public void verification(Integer id, Integer userId,Integer companyId) {
        OrderVO vo = this.getById(id, userId, companyId);
        vo.setCompanyVO(companyClient.getById(companyId));
        OrderCommand command = whAdaptorClient.add(id, vo);
        command.setSubmitTime(DateUtil.convertDateToString(new Date()));
        this.update(id,command,userId,companyId);
    }
}
